<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
        <el-row>
          <el-col :span="4">
            <el-form-item label="公司名">
              <el-input
                size="default"
                placeholder="请输入公司名"
                style="max-width: 180px"
                v-model="state.name"
              >
              </el-input> </el-form-item
          ></el-col>
          <el-col :span="5">
            <el-form-item label="选择日期">
              <el-date-picker
                v-model="state.time"
                size="default"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                type="date"
                placeholder="请选择日期"
              /> </el-form-item
          ></el-col>
          <el-col :span="2">
            <el-form-item label="  ">
              <el-button
                size="default"
                type="primary"
                class="ml10"
                @click="getTableData()"
              >
                <el-icon>
                  <ele-Search />
                </el-icon>
                查询
              </el-button></el-form-item
            ></el-col
          >
          <el-col :span="2">
            <el-form-item label="  ">
              <el-button size="sm" type="success" class="ml10" @click="onOpenAdd('add')">
                <el-icon>
                  <ele-FolderAdd />
                </el-icon>
                新增订单
              </el-button></el-form-item
            ></el-col
          >
          <el-col :span="6">
            <div class="grid-content ep-bg-purple" />
          </el-col>
        </el-row>
      </div>
      <el-table
        :data="state.tableData.data"
        v-loading="state.tableData.loading"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="序号"
          show-overflow-tooltip
          align="center"
        ></el-table-column>
        <el-table-column
          prop="company"
          label="客户名称"
          show-overflow-tooltip
          align="center"
        ></el-table-column>
        <el-table-column
          prop="orderTime"
          label="日期"
          show-overflow-tooltip
          align="center"
        ></el-table-column>
        <el-table-column
          prop="goods"
          label="商品"
          width="400"
          show-overflow-tooltip
          align="center"
        ></el-table-column>
        <el-table-column
          prop="amount"
          label="金额"
          show-overflow-tooltip
          align="center"
        ></el-table-column>
        <el-table-column
          prop="goodsList"
          label="商品详情"
          show-overflow-tooltip
          align="center"
        ></el-table-column>

        <el-table-column prop="note" label="备注" show-overflow-tooltip align="center">
        </el-table-column>

        <el-table-column label="操作" width="160" align="center">
          <template #default="scope">
            <el-button
              :disabled="scope.row.userName === 'admin'"
              size="small"
              text
              type="primary"
              @click="onOpenEdit('edit', scope.row)"
              >修改</el-button
            >
            <el-button
              :disabled="scope.row.userName === 'admin'"
              size="small"
              text
              type="primary"
              @click="download(scope.row.id)"
              >打印</el-button
            >
            <el-button
              :disabled="scope.row.userName === 'admin'"
              size="small"
              text
              type="primary"
              @click="onRowDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="onHandleSizeChange"
        @current-change="onHandleCurrentChange"
        class="mt15"
        :pager-count="5"
        :page-sizes="[10, 20, 30]"
        v-model:current-page="state.tableData.param.pageNum"
        background
        v-model:page-size="state.tableData.param.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <orderDialog ref="orderDialogFormRef" @refresh="getTableData()" />
  </div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { get_list, del, get_data_download } from "/@/api/order/index";
// 引入组件
const orderDialog = defineAsyncComponent(() => import("/@/views/order/dialog.vue"));
// 定义变量内容
const orderDialogFormRef = ref();
const state = reactive<any>({
  time: "",
  name: "",
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  get_list(
    state.name,
    state.time,
    state.tableData.param.pageNum,
    state.tableData.param.pageSize
  ).then((res) => {
    state.tableData.loading = false;
    state.tableData.data = res.data.records;
    state.tableData.total = res.data.total;
  });
};
// 打开新增用户弹窗
const onOpenAdd = (type: string) => {
  orderDialogFormRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEdit = (type: string, row: any) => {
  orderDialogFormRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row: any) => {
  ElMessageBox.confirm(
    `此操作将永久删除订单：“${row.company}:${row.orderTime}”，是否继续?`,
    "提示",
    {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      del(row.id).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.msg);
          getTableData();
        }
      });
    })
    .catch(() => {});
};
const download = (id: any) => {
  get_data_download(id).then((res: any) => {
    if (res.size == 76) {
      ElMessage.error("查询的车辆或时间段 没有数据");
    } else {
      ElMessage.success("下载完成，正在打开");
      // console.log(res);
      const blob = new Blob([res]);
      const element = document.createElement("a");
      // 配置文件名，默认是 txt，可以自定义，比如：操作日志.csv
      element.download = "销售单" + ".xls";
      element.style.display = "none";
      element.href = URL.createObjectURL(blob);
      document.body.append(element);
      element.click();
      URL.revokeObjectURL(element.href);
      document.body.removeChild(element);
    }
  });
};

// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
